package com.jn.one.controller;

import com.jn.one.mapper.HealthMapper;
import com.jn.one.model.Health;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
public class HealthController {
    @Autowired
    private HealthMapper healthMapper;

    @PostMapping("/two")
    public int healthTwo(@RequestBody String s){
        //String queryString = newData.getQueryString();
        //String name = newData.getParameter("name");
        try {
            s = URLDecoder.decode(s, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String[] split = s.split("&");
        Health health = new Health();
        for (String sp : split) {
            int indexOf = sp.indexOf("=");
            String substring = sp.substring(indexOf+1);
            switch (sp.substring(0, indexOf)) {
                case"name":
                    health.setName(substring);
                    break;
                case"gender":
                    health.setGender(substring);
                    break;
                case"age":
                    int i = Integer.parseInt(substring);
                    health.setAge(i);
                    break;
                //int i=Integer.parseInt(health.setAge(sp.substring(sp.indexOf("="))));
                case "phone":
                    int a = Integer.parseInt(substring);
                    health.setPhone(a);
                    break;
                case"cars":
                    health.setCars(substring);
                    break;
                case"company":
                    health.setCompany(substring);
                    break;
                case "height":
                    int b = Integer.parseInt(substring);
                    health.setHeight(b);
                    break;
                case "weight":
                    int c = Integer.parseInt(substring);
                    health.setWeight(c);
                    break;
                case "yaowei":
                    int d = Integer.parseInt(substring);
                    health.setYaowei(d);
                    break;
                case "mybmi":
                    int e = Integer.parseInt(substring);
                    health.setMybmi(e);
                    break;
                case"fill":
                    health.setFill(substring);
                    break;
                case"mill":
                    health.setMill(substring);
                    break;
                case"hproblem":
                    health.setHproblem(substring);
                    break;
                case"tydlpl":
                    health.setTydlpl(substring);
                    break;
                case"ysxg":
                    health.setYsxg(substring);
                    break;
                case"xyqk":
                    health.setXyqk(substring);
                    break;
                case"yjpl":
                    health.setYjpl(substring);
                    break;
                case"kpjz":
                    health.setKpjz(substring);
                    break;
                case"jkgl":
                    health.setJkgl(substring);
                    break;
            }
        }
//        return healthMapper.insertall(health.getName(),health.getAge(),health.getGender(),health.getPhone(),health.getCars(),
//                health.getCompany(),health.getHeight(),health.getWeight(),health.getYaowei(),health.getMybmi(),health.getFill(),health.getMill(),health.getHproblem(),
//                health.getTydlpl(),health.getYsxg(),health.getXyqk(),health.getYjpl(),health.getKpjz(),health.getJkgl());
        return healthMapper.insertall2(health);
    }
}
